package com.ruoyi.place.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.place.domain.PlaceKfjzzx;
import com.ruoyi.place.domain.bo.PlaceKfjzzxBo;
import com.ruoyi.place.domain.vo.PlaceKfjzzxVo;
import com.ruoyi.place.mapper.PlaceKfjzzxMapper;
import com.ruoyi.place.service.IPlaceKfjzzxService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 康复救助中心Service业务层处理
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@RequiredArgsConstructor
@Service
public class PlaceKfjzzxServiceImpl implements IPlaceKfjzzxService {

    private final PlaceKfjzzxMapper baseMapper;

    /**
     * 查询康复救助中心
     */
    @Override
    public PlaceKfjzzxVo queryById(Long kfjzzxId) {
        return baseMapper.selectVoById(kfjzzxId);
    }

    /**
     * 查询康复救助中心列表
     */
    @Override
    public TableDataInfo<PlaceKfjzzxVo> queryPageList(PlaceKfjzzxBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PlaceKfjzzx> lqw = buildQueryWrapper(bo);
        Page<PlaceKfjzzxVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询康复救助中心列表
     */
    @Override
    public List<PlaceKfjzzxVo> queryList(PlaceKfjzzxBo bo) {
        LambdaQueryWrapper<PlaceKfjzzx> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PlaceKfjzzx> buildQueryWrapper(PlaceKfjzzxBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PlaceKfjzzx> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), PlaceKfjzzx::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增康复救助中心
     */
    @Override
    public Boolean insertByBo(PlaceKfjzzxBo bo) {
        PlaceKfjzzx add = BeanUtil.toBean(bo, PlaceKfjzzx.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setKfjzzxId(add.getKfjzzxId());
        }
        return flag;
    }

    /**
     * 修改康复救助中心
     */
    @Override
    public Boolean updateByBo(PlaceKfjzzxBo bo) {
        PlaceKfjzzx update = BeanUtil.toBean(bo, PlaceKfjzzx.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PlaceKfjzzx entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除康复救助中心
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
